<?php
$smarty->configLoad($lang . '/rename.module.php');
$language = parse_ini_file($lang . '/breadcrumb.module.php', true);
$breadcrumb = $language['rename'];

if(isset($_POST['submit'])) {

	$error = '';
	$sql = 'SELECT * FROM '.DB_LOGIN.'.account_data WHERE name = :name';
	$dbh = $connection->prepare($sql);
	$dbh->bindValue(':name', $_POST['new_username']);
	$dbh->execute();
	$num_acc = $dbh->rowCount();

	if(empty($_POST['old_username']) || empty($_POST['new_username'])) {
		$error = 1;
	}
	if($num_acc >= 1) {
		$error = 1;
	}

	if(empty($error)) {
		$sql = 'UPDATE '.DB_LOGIN.'.account_data SET name = :name WHERE name = :oldname';
		$dbh = $connection->prepare($sql);
		$dbh->bindValue(':name', $_POST['new_username']);
		$dbh->bindValue(':oldname', $_POST['old_username']);
		$dbh->execute();

		$sql = 'UPDATE '.DB_GAME.'.players SET account_name = :name WHERE account_name = :oldname';
		$dbh = $connection->prepare($sql);
		$dbh->bindValue(':name', $_POST['new_username']);
		$dbh->bindValue(':oldname', $_POST['old_username']);
		$dbh->execute();

		$main->jqmodal('edited');
		echo '<meta http-equiv="refresh" content="5; URL=index.php?mod=rename">';
	}

}
$template = 'rename/index.tpl';
?>
 